Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
Re-rendering issues when loading all pdf pages in "react-pdf"

I am trying to load all the pages but it goes into infinate loop(basically re-rendering). React.memo is not helping me here.

const PdfRender = React.memo(({ pdfURL, pages, setPages }: any) => (
  <Document
    file={{
      url: pdfURL,
    }}
    onLoadSuccess={(info) => {
      setPages(new Array(info._pdfInfo.numPages).fill(info._pdfInfo.numPages));
    }}
  >
    {pages.map((_: any, i: number) => {
      return <Page pageNumber={i + 1} />;
    })}
  </Document>
));

I think, the re-rendering behaviour is logically correct because onLoadSuccess will get fire when the content is updated. As a result setPages and pages also get updated and pages re-renders the same pages again and onLoadSuccess will get fire again.(a loop basically)

But there has be any solutions to this.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Maybe you need to add a custom props check in the second argument of React.memo.

The memo HOC will only shallowly compare complex objects in the props object.

Example:

const PdfRender = React.memo(({ pdfURL, pages, setPages }: any) => (
  <Document
    file={{
      url: pdfURL,
    }}
    onLoadSuccess={(info) => {
      setPages(new Array(info._pdfInfo.numPages).fill(info._pdfInfo.numPages));
    }}
  >
    {pages.map((_: any, i: number) => {
      return <Page pageNumber={i + 1} />;
    })}
  </Document>
), (prevProps, nextProps) => {
  /*
  return true if passing nextProps to render would return
  the same result as passing prevProps to render,
  otherwise return false
  */
});

SOURCE

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda